<?php
// +----------------------------------------------------------------------
// | 鸿运集团 [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use \think\Session;
class Dinner extends Permissions
{
    //餐桌管理
    public function desk()
	{
		$where = []; 
		if(!empty(input('store_id'))){
			 $where['d.store_id'] = input('store_id');
		}
        if(!empty(input('area_id'))){
			 $where['d.area_id'] = input('area_id');
		}
		if(!empty(input('number'))){
			 $where['d.number'] = input('number');
		}
		$dinner_list = db('dinner')->alias('d')
		    ->join('store s','d.store_id = s.id','LEFT')
			->join('area a','d.area_id = a.id','LEFT')
		    ->where($where)
			->order("d.add_time desc")
			->field('d.*,s.store_name,a.area_name')
			->paginate(10,false,['query'=>request()->param()]);
		$this->assign('dinner_list',$dinner_list);
		
	    //查询所有门店
		$list = db('store')->field('store_name,id')->select();
		$this->assign('list',$list);
		
		//查询所有餐桌分类
		$area = db('area')->field('area_name,id')->select();
		$this->assign('area',$area);
		$this->assign('store_id',input('store_id'));
		$this->assign('area_id',input('area_id'));
		$this->assign('number',input('number'));
		return $this->fetch();
	}
	
	 //餐桌分类
    public function area()
	{
		$where = []; 
		if(!empty(input('area_name'))){
			 $where['a.area_name'] = input('area_name');
		}
        if(!empty(input('store_id'))){
			 $where['a.store_id'] = input('store_id');
		}
		//查询餐桌数量
		$total = db('dinner')->group('area_id')->field('count(id) as num,area_id')->buildSql(); 
		$area_list = db('area')->alias('a')
		    ->join('store s','a.store_id = s.id','LEFT')
			->join("$total d",'d.area_id = a.id','LEFT')
		    ->where($where)
			->order("a.add_time desc")
			->field('a.*,s.store_name,d.num')
			->paginate(10,false,['query'=>request()->param()]);
		
		$this->assign('area_list',$area_list);
		//查询所有门店
		$list = db('store')->field('store_name,id')->select();
		$this->assign('list',$list);
		$this->assign('area_name',input('area_name'));
		$this->assign('store_id',input('store_id'));
		return $this->fetch();
	}
	
	//添加餐桌分类
	public function addarea()
	{
		//查询所有门店
		$list = db('store')->field('store_name,id')->select();
		$this->assign('list',$list);
		
		if(request()->isPost()){
			$data = $_POST;  
			$data['add_time'] = now_time();
			//判断分类名称是否有添加过
			$area_name = db('area')->where('area_name',$data['area_name'])->find();
			if($area_name){
				return $this->error('该分类名称已经添加过');
			}
			//判断排序是否有添加过
			$px = db('area')->where('sort',$data['sort'])->find();
			if($px){
				return $this->error('该排序已经添加过');
			}
			$res = db('area')->insert($data);
			if($res){ 
				return $this->success('新增成功','admin/dinner/area');
	        }else{ 
				return $this->error('新增失败');
	        }
		}
		return $this->fetch();
	}
	
	//编辑餐桌分类
	public function editarea()
	{
		//查询所有门店
		$list = db('store')->field('store_name,id')->select();
		$this->assign('list',$list);
		
		//查询餐桌信息
		$info = db('area')->where('id',input('id'))->find();
		$this->assign('info',$info);
		
		if(request()->isPost()){ 
			$data = $_POST;   
			//判断分类名称是否有添加过 
			if($data['area_name'] != $info['area_name']){
				$area_name = db('area')->where('area_name',$data['area_name'])->find();
				if($area_name){
					return $this->error('该分类名称已经添加过');
				}
			}
			//判断排序是否有添加过 
			if($data['sort'] != $info['sort']){
				$px = db('area')->where('sort',$data['sort'])->find();
				if($px){
					return $this->error('该排序已经添加过');
				}
			}
			$res = db('area')->where('id',$data['id'])->update($data);
			if($res){ 
				return $this->success('编辑成功','admin/dinner/area');
	        }else{ 
				return $this->error('编辑失败');
			}
	    }
		return $this->fetch();
	}
	
	//删除餐桌分类
	public function dlt()
	{
		$res = db('area')->where('id',input('id'))->delete(); 
		if($res){
			return $this->success('删除成功','admin/dinner/area');
		}else{
			return $this->error('删除失败');
		}
	}
	
	//新增餐桌
	public function adddinner()
	{
		 //查询所有门店
		$list = db('store')->field('store_name,id')->select();
		$this->assign('list',$list); 
		
		if(request()->isPost()){
			$data = $_POST;  
			$data['add_time'] = now_time();
			//判断餐桌名称是否有添加过
			$area_name = db('dinner')->where('dinner_name',$data['dinner_name'])->find();
			if($area_name){
				return $this->error('该餐桌名称已经添加过');
			}
			//判断桌台编号是否有添加过
			$px = db('dinner')->where('number',$data['number'])->find();
			if($px){
				return $this->error('该桌台编号已经添加过');
			}
			$res = db('dinner')->insert($data);
			if($res){ 
				return $this->success('新增成功','admin/dinner/desk');
	        }else{ 
				return $this->error('新增失败');
	        }
		}
		return $this->fetch();
	}
	
	//获取门店下的餐桌分类
	public function get_area()
	{
		$store_id = input('store_id');
		$room = db('area')->where('store_id',$store_id)->field('area_name,id')->select();
		return json(["data"=>$room]);
	}
	
	//编辑餐桌管理
	public function editdinner()
	{
		//查看餐桌管理信息
		$info = db('dinner')->where('id',input('id'))->find();
		$this->assign('info',$info); 
		
		//查询所有门店
		$list = db('store')->field('store_name,id')->select();
		$this->assign('list',$list); 
		//查询所有餐桌分类 
		$area = db('area')->where('store_id',$info['store_id'])->field('area_name,id')->select();
		$this->assign('area',$area); 
		
		if(request()->isPost()){
			$data = $_POST;   
			//判断餐桌名称是否有添加过
			if($data['dinner_name'] != $info['dinner_name']){
				$area_name = db('dinner')->where('dinner_name',$data['dinner_name'])->find();
				if($area_name){
					return $this->error('该餐桌名称已经添加过');
				}
			}
			//判断桌台编号是否有添加过
			if($data['number'] != $info['number']){
				$px = db('dinner')->where('number',$data['number'])->find();
				if($px){
					return $this->error('该桌台编号已经添加过');
				}
			}
			$res = db('dinner')->where('id',$data['id'])->update($data);
			if($res){ 
				return $this->success('编辑成功','admin/dinner/desk');
	        }else{ 
				return $this->error('编辑失败');
	        }
		}
		return $this->fetch();
	}

    //删除餐桌管理
	public function dlt2()
	{
		$res = db('dinner')->where('id',input('id'))->delete(); 
		if($res){
			return $this->success('删除成功','admin/dinner/desk');
		}else{
			return $this->error('删除失败');
		}
	}
	
	//房台二维码
	public function ewm()
	{
		$this->assign('number',input('number'));
		$url=config("WEB_DOMAIN")."/index/member/diancan?room_id=".input("number")."&store_id=".input("store_id");
		$this->assign('name',input('name'));
		$this->assign('url',$url);
		return $this->fetch();
	}
	
	//餐桌管理
    public function biaoqian()
	{
		$list=db("biaoqian")->select();	
		return $this->fetch("",['list'=>$list]);
	}
	//菜品管理
	public function caipin(){
		    $list = db("caipin")->order('create_time desc')->paginate(10,false,['query'=>$this->request->param()]);
			$stores=db("store")->select();
			return $this->fetch("",['list'=>$list,'stores'=>$stores]);	
	}
	//菜品
	public function caipin_type(){
		$list=db("caipin_type")->select();
		return $this->fetch("",['list'=>$list]);
	}
	//菜品分类
	public function add_caipin_type($id=""){
		$type=null;
		if($_POST){
			$data['name']=input("name");
			$data['sort']=input("sort");
			$res=true;
			if($id){
				$res=db("caipin_type")->where("id",$id)->update($data);
			}else{
				$res=db("caipin_type")->insert($data);
			}
			if($res){
				return json(['code'=>200,'msg'=>'操作成功']);
			}else{
				return json(['code'=>400,'msg'=>'操作失败']);
			}
		}
		if($id){
			$type=db("caipin_type")->where("id",$id)->find();
		}
		$list=db("caipin_type")->select();
		return $this->fetch("",['type'=>$type]);
	}
	//添加菜品
	public function add_caipin($id=""){
		if($_POST){
			if($id){
				$res=db("caipin")->where("id",$id)->update(input());	
			}else{
				$res=db("caipin")->insert(input());	
			}
			if($res){
				return json(['code'=>200,'msg'=>'操作成功']);
			}
			return json(['code'=>400,'msg'=>'操作失败']);
		}
		$caipin=null;
		if($id){
			$caipin=db("caipin")->where("id",$id)->find();
		}
		$stores=db("store")->select();
		$list=db("caipin_type")->select();
		$bq=db("biaoqian")->select();
		return $this->fetch("",['list'=>$list,'stores'=>$stores,'bq'=>$bq,'caipin'=>$caipin]);	
	}
	//添加标签
	public function add_biaoqian(){
		if($_POST){
			$data['name']=input("name");
			$data['sort']=input("sort");
			$res=db("biaoqian")->insert($data);
			if($res){
				return json(['code'=>200,'msg'=>'操作成功']);
			}
			return json(['code'=>400,'msg'=>'操作失败']);
		}
		return $this->fetch("");
	}
	public function edit_biaoqian(){
		if($_POST){
			$data['name']=input("name");
			$data['sort']=input("sort");
			$res=db("biaoqian")->where("id",input("id"))->update($data);
			if($res){
				return json(['code'=>200,'msg'=>'操作成功']);
			}
			return json(['code'=>400,'msg'=>'操作失败']);
		}
		$bq=db("biaoqian")->where("id",input("id"))->find();
		return $this->fetch("",['bq'=>$bq]);
	}
	//销量
	public function xiaoliang($store_id="",$start_time=''){
		$where=[];
		if($store_id){
			$where['a.store_id']=$store_id;
		}
		if($start_time){
			//$where['a.add_time']=['between',["$start_time 00:00:00",["$start_time 23:59:59"]]];
		}
		$list=db("caipin")->alias("a")
		->join("store b","a.store_id=b.id","left")
		->join("caipin_type c","a.type_id=b.id","left")
		->field("a.*,b.store_name,c.name as type_name")
		->where($where)
		->paginate(10,false,['query'=>$this->request->param()]);
		$store=db("store")->select();
		return $this->fetch("",['list'=>$list,"store"=>$store,"store_id"=>$store_id,'start_time'=>$start_time]);
	}
}
